<? 
	$title="User List"; 
	$pageid="4";
	$accessid=",a10,";

	require("required/dbconnect.php"); 
	require("required/function.php");
	require("required/header.php");
	
	checkAccess("allow_system_management",1);
	
	if(isset($_POST['btnupdate']))
	{
		$api_key="I8QII-MKi74-4DGKj";
		
		$get_main=mysql_query("select * from os_currency where is_main=1")or die(mysql_error());
		
		if(mysql_num_rows($get_main)==0)
		{
			header("Location:currency_setting.php?action=4");
		}
		else
		{
			$from=trim(mysql_result($get_main,0,"currency_code"));
			$rate=trim(mysql_result($get_main,0,"rate"));
			$get_currency=mysql_query("select * from os_currency order by is_main desc, country asc")or die(mysql_error());
			
			while($a=mysql_fetch_array($get_currency,MYSQL_ASSOC))
			{		
				$id = $a['id'];
				$to = trim($a['currency_code']);
				$url = "http://www.exchangerate-api.com/".$from."/".$to."/".$rate."?k=$api_key";
				
				$result = file_get_contents($url);
				
				mysql_query("update os_currency set rate='$result' where id='$id'");
			}
			
			unset($_SESSION['currency_id']);
			//header("Location:currency_setting.php?action=5");
		}	
	}
	
?>
<script src="required/js/jquery-1.4.2.js"></script>
<script src="required/js/common_javascript.js"></script>
<? 
	if(!empty($_GET['action']))
	{
		switch($_GET['action'])
		{
			case 1:
				$msg="<div class='dw_success'>Data inserted</div>";
				break;
			case 2:
				$msg="<div class='dw_success'>Data Deleted</div>";
				break;
			case 3:
				$msg="<div class='dw_success'>Data Updated</div>";
				break;
			case 4:
				$msg="<div class='dw_failed'>Update Failed! Please set a main currency.</div>";
				break;
			case 5:
				$msg="<div class='dw_success'>Currency Rate Updated</div>";
				break;				
			default:
				$msg="";
				break;
		}
		echo "<script>displayMsg('divMsg')</script>";	
	}else{
		$msg="";
	}
				
	$get_currency=mysql_query("select * from os_currency order by is_main desc, country asc")or die(mysql_error());
	
	
?>
<table width="100%"  cellpadding="0" cellspacing="5" height="300">
<tr>
	<td valign="top">
		<div class="title">Currency Setting</div>
        <br>
        
		<div id="divMsg"><?= $msg ?></div>
		<table width="100%">
			<tr class="table_header">
            	<td width="4%">No.</td>
           	  <td width="57%">Currency Code</td>
           	  <td width="19%">Currency Name</td>
           	  <td width="11%">Rate</td>
           	  <td width="9%">Action</td>
          </tr>
            <form method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
			<?
				$no=1;
				while($a=mysql_fetch_array($get_currency,MYSQL_ASSOC))
				{
					if($no%2==0)
					{
						$class1="table_item_even";
						$class2="table_item_odd";
					}
					else
					{
						$class1="table_item_odd";
						$class2="table_item_even";
					}
					
					
			?>
			<input type="hidden" value="<?= $a['id'] ?>" name="id_<?= $no ?>" >
			<tr height="20" valign="top" class="<?= $class1 ?>"  onmouseover="this.className='table_item_mouseover'" 
                            onmouseout="this.className='<?= $class1 ?>'">
				<Td><?= $no ?></Td>
                <td><?= $a['currency_code'] ?>
				<?
					if($a['is_main']==1)
					{
				?>
					(Main Currency)
				<?	
					}
				?>
				</td>
				<td><?= $a['country'] ?></td>
				<td><?= $a['rate'] ?></td>
                <Td align="center">
                <?
					if($a['is_main']!=1)
					{
				?>
                <a href="currency_edit.php?id=<?= $a['id'] ?>"><img src="images/icons/edit.gif" border="0"></a>
                <a href="currency_delete.php?id=<?= $a['id'] ?>"><img src="images/icons/delete.gif" border="0"></a>    
                <?	
					}
				?>        
                </Td>
			</tr>
				
			<?
				$no++;
				}
			?>
			<input type="hidden" value="<?= $no ?>" name="totalcurrency">
			<tr>
				<td></td><td>
                <input type="button" value="Add" class="button" onclick="window.open('currency_add.php','_self')">
                <input type="submit" value="Live Update" class="button" name="btnupdate">
                <br />
                "Live Update" allowed you to update the rate on selected currencies
                </td>
			</tr>
			</form>
		</table>
			
		
		
	</td>
</tr>
	
	
</table>
<? require("required/footer.php"); ?>

